package net.bezrabotnih.server.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooEntity(identifierType = String.class,identifierField="id")
public class Comment {
	
	private String text;
	
	private Boolean hidden=false;
	
	private String userAccountId;
	
	private String jobId;
	
	@Transient
	private UserAccount userAccount;
	
	private Date created=new Date();
	
	
	
	


	public UserAccount getUserAccount() {
    	if (this.userAccountId != null) {
            if (userAccount == null || userAccount.getId() != this.userAccountId) {
            	userAccount = UserAccount.findUserAccount(this.userAccountId);
            }
        } else {
        	userAccount = null;
        }
        return userAccount;
    }
	
	public static List<Comment> findCommentsByJobId(String jobId){
		
	        try {
	            return (List<Comment>) entityManager().createQuery("SELECT o FROM Comment o WHERE o.jobId=:jobId ORDER BY o.created ASC").setParameter("jobId", jobId).getResultList();
	        } catch (org.springframework.dao.EmptyResultDataAccessException e) {
	            return null;
	        } catch (javax.persistence.NoResultException e) {
	            return null;
	        }
	}
	
	public static List<Comment> findComments(List<String> ids){
		

		 if (ids == null || ids.size() == 0) return null;
	        try {
	            return (List<Comment>) entityManager().createQuery("SELECT o FROM Comment o WHERE o.id MEMBER OF :ids  ORDER BY o.created ASC").setParameter("ids", ids).getResultList();
	        } catch (org.springframework.dao.EmptyResultDataAccessException e) {
	            return null;
	        } catch (javax.persistence.NoResultException e) {
	            return null;
	        }
	}
	

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}



	public String getJobId() {
		return jobId;
	}



	public void setJobId(String jobId) {
//		System.out.println("jobId:"+jobId);
		this.jobId = jobId;
	}
	
}
